<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Trans extends CI_Controller {

	public function __construct() {
		parent::__construct();
	}

	public function index(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
        $this->load->model('kategori','',true);
		$this->load->model('transaksi','',true);
		$this->load->model('barang','',true);
		$data['barang']=$this->barang->get_list();
		$data['transaksi'] = $this->transaksi->get_all();
		$data['tipe']=$this->transaksi->get_tipe();
		$menu['kategori'] = $this->kategori->get_all();
		$this->load->view('header');
		$this->load->view('menu',$menu);
		$this->load->view('transaksi',$data);
		$this->load->view('footer');
	}

	public function insert(){
		$tgl = $this->input->post('tanggal');
		list($d, $m, $y) = explode ( '/', $tgl);
		$tanggal = "$y-$m-$d";

		$idbarang=""; 
		$tmp = $this->input->post('nama');
		$this->load->model('barang','',true);
		$nama = $this->barang->get_by_nama($tmp);
		foreach ($nama->result() as $row){
			$idbarang=$row->idbarang;
		}


		$data = array(
				'tanggal' 		=> $tanggal,
				'idbarang' 		=> $idbarang,
				'jumlah' 		=> $this->input->post('jumlah'),
				'idtransaksi'	=> $this->input->post('idtransaksi')
			);
		$this->load->model('transaksi','',true);
		$this->transaksi->insert($data);
		redirect('trans');
	}

	public function delete($data){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('transaksi','',true);
		$this->transaksi->delete($data);
		redirect('trans');
	}

	public function edit($iddetail){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('kategori','',true);
		$this->load->model('transaksi','',true);
		$data['detail'] = $this->transaksi->get_by_id($iddetail);
		$data['transaksi'] = $this->transaksi->get_all();
		$data['tipe']=$this->transaksi->get_tipe();
		$menu['kategori'] = $this->kategori->get_all();
		$this->load->view('header');
		$this->load->view('menu',$menu);
		$this->load->view('transaksi',$data);
		$this->load->view('footer');
	}

	public function update(){
		$tgl = $this->input->post('tanggal');
		list($d, $m, $y) = explode ( '/', $tgl);
		$tanggal = "$y-$m-$d";

		$idbarang=""; 
		$tmp = $this->input->post('nama');
		$this->load->model('barang','',true);
		$nama = $this->barang->get_by_nama($tmp);
		foreach ($nama->result() as $row){
			$idbarang=$row->idbarang;
		}


		$data = array(
				'tanggal' 		=> $tanggal,
				'idbarang' 		=> $idbarang,
				'jumlah' 		=> $this->input->post('jumlah'),
				'idtransaksi'	=> $this->input->post('idtransaksi')
			);
		$id = $this->input->post('iddetail');
		$this->load->model('transaksi','',true);
		$this->transaksi->update($id,$data);
		redirect('trans');
	}
}